/*
 * @lc app=leetcode.cn id=203 lang=cpp
 *
 * [203] 移除链表元素
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        while(head!=NULL && head->val == val)
            head = head->next;
        ListNode* p = head;
        if(p != NULL)
        {
            while(p->next != NULL)
            {
                if(p->next->val == val)
                {
                    p->next = p->next->next;
                }
                else
                    p = p->next;
            }
        }
        return head;
    }
};
// @lc code=end

